package com.sqstudio.util{
	import flash.display.*;
	/**
	 *画扇形
	 * @author Nestor
	 * 
	 */	
	public class SectorGraph extends Sprite {
		private var _mask:Shape;
		
		public function SectorGraph() {
			// constructor code
			this.init();
		}
		
		private function init():void {
			this._mask = new Shape();
			this.addChild(this._mask);
			this.mask = this._mask;
		}
		
		public function drawSector(x:Number, y:Number, radius:Number, beginAngle:Number, endAngle:Number, color:uint = 0xffffff, alpha:Number = 1):void {
			
			this.graphics.clear();
			this.graphics.beginFill(color, alpha);
			this.graphics.drawCircle(x, y, radius);
			this.graphics.endFill();
			
			this._mask.graphics.clear();
			this._mask.graphics.beginFill(0);
			
			// 以90度为单位角度，蒙板半径为扇形半径的根号2倍
			var maskRadius:Number = radius * Math.SQRT2;
			// 第一个位置
			var beginRadian:Number = Calc.getRadian(beginAngle);
			var beginX:Number = x + Math.cos(beginRadian) * maskRadius;
			var beginY:Number = y + Math.sin(beginRadian) * maskRadius;
			this._mask.graphics.moveTo(beginX, beginY);
			// 以90度为单位角度，计算下一个位置
			var offsetAngle:Number = Math.abs(endAngle - beginAngle);
			var n:uint = uint(offsetAngle / 90);
			var i:uint;
			for (i = 1; i <= n; i++) {
				var radian:Number = beginRadian + Math.PI * .5 * i;
				var tx:Number = x + Math.cos(radian) * maskRadius;
				var ty:Number = y + Math.sin(radian) * maskRadius;
				this._mask.graphics.lineTo(tx, ty);
			}
			// 最后一个位置
			var endRadian:Number = Calc.getRadian(endAngle);
			var endX:Number = x + Math.cos(endRadian) * maskRadius;
			var endY:Number = y + Math.sin(endRadian) * maskRadius;
			this._mask.graphics.lineTo(endX, endY);
			// 圆心
			this._mask.graphics.lineTo(x, y);
			this._mask.graphics.endFill();
		}
		
	}
	
}

class Calc {
	public static function getRadian(angle:Number):Number {
		return Math.PI * angle / 180;
	}
}